import React, { useState } from 'react';
import { copyToClipboard, generateShareLink } from '../utils/shareUtils';

interface ShareButtonProps {
  text: string;
  inputText: string;
}

const ShareButton: React.FC<ShareButtonProps> = ({ text, inputText }) => {
  const [isCopied, setIsCopied] = useState(false);
  
  const handleShare = async () => {
    try {
      if (navigator.share) {
        await navigator.share({
          title: '女朋友潜台词翻译结果',
          text: `她说: "${inputText}"\n潜台词是: ${text.split('潜台词是: ')[1]}`,
          url: window.location.href
        });
      } else {
        const shareLink = generateShareLink(inputText, text);
        copyToClipboard(shareLink);
        setIsCopied(true);
        setTimeout(() => setIsCopied(false), 2000);
      }
    } catch (err) {
      console.error('分享失败:', err);
    }
  };

  return (
    <button onClick={handleShare} className="share-button">
      {isCopied ? '✅ 已复制!' : '📤 分享结果'}
    </button>
  );
};

export default ShareButton;